﻿using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Common.DTO.ShippingnoteDTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace SalesManagementCommon.API.SalesManagementCommons.Shippingnotess
{
    public class ShippingnoteEditorHandler : IRequestHandler<ShippingnoteEditorCommand, Result<ShippintprintDTO>>
    {
        private readonly IBaseRepository<Shippingnote> shippingnoteRepository;
        private readonly IBaseRepository<SalesOrder> salesOrderRepository;
        private readonly IBaseRepository<SalesOrderMaterial> salesOrderMaterialRepository;
        private readonly IBaseRepository<Materialdelivery> materialdeliveryRepository;

        public ShippingnoteEditorHandler(IBaseRepository<Shippingnote> shippingnoteRepository, IBaseRepository<SalesOrder> salesOrderRepository, IBaseRepository<SalesOrderMaterial> salesOrderMaterialRepository, IBaseRepository<Materialdelivery> materialdeliveryRepository)
        {
            this.shippingnoteRepository = shippingnoteRepository;
            this.salesOrderRepository = salesOrderRepository;
            this.salesOrderMaterialRepository = salesOrderMaterialRepository;
            this.materialdeliveryRepository = materialdeliveryRepository;
        }

        public async Task<Result<ShippintprintDTO>> Handle(ShippingnoteEditorCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<ShippintprintDTO>();
            try
            {
                var shippings = await (await shippingnoteRepository.GetAll()).ToListAsync();
                var materialdelivery = await (await materialdeliveryRepository.GetAll()).ToListAsync();
                var list = (from a in shippings
                            where a.ShippingnoteId == request.ShippingnoteId
                            select new ShippintprintDTO()
                            {
                                ShippingnoteId = a.ShippingnoteId,
                                DocumentNumber = a.DocumentNumber,
                                Documentdate = a.Documentdate,
                                DocumentStatus = a.DocumentStatus,
                                BusinessStatus = a.BusinessStatus,
                                BusinessType = a.BusinessType,
                                Stash = a.Stash,
                                Customername = a.Customername,
                                Warehouseentry = a.Warehouseentry,
                                Deliverydate = a.Deliverydate,
                                Consignee = a.Consignee,
                                ConsigneePhone = a.ConsigneePhone,
                                ConsigneeAddress = a.ConsigneeAddress,
                                ConsigneeInfo = a.ConsigneeInfo,
                                Freight = a.Freight,
                                Handlingcharges = a.Handlingcharges,
                                Serialnumber = a.Serialnumber,
                                MaterialdeliveryDTOs = new List<MaterialdeliveryDTO>()
                            }).FirstOrDefault();
                if (list != null)
                {
                    list.MaterialdeliveryDTOs = (from a in materialdelivery
                                                 where a.Serialnumber == list.Serialnumber
                                                 select new MaterialdeliveryDTO()
                                                 {
                                                     MaterialdeliveryId = a.MaterialdeliveryId,
                                                     DocumentNumber = a.DocumentNumber,
                                                     ItemBianma = a.ItemBianma,
                                                     MaterialName = a.MaterialName,
                                                     Quantity = a.Quantity,
                                                     Numberpack = a.Numberpack,
                                                     ShippingnoteRemark = a.ShippingnoteRemark,
                                                     Serialnumber = a.Serialnumber,
                                                 }).ToList();
                }
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;

        }


    }
}
